﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Linq.Expressions;

namespace HQWeb.View
{
    public partial class CustomizeView : BasePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
            if (!IsPostBack)
            {
                if (Request.Form["id"] != null)
                {
                    string strID = Request.Form["id"];
                    if (Request["projecttype"] != null)
                    {
                        HIdProjectType.Value = Request["projecttype"].Trim();
                    }
                    hidID.Value = strID;
                    BindView();
                }
                else
                {

                    bindProjectFile(Request["projectid"]);
                }

                #region 文件操作按钮
                if (Convert.ToBoolean(Session["FILE_EDIT"]) == false)
                {
                  //  mDeleteVideo.Style["display"] = "none";
                    //TdDeletePhoto.Disabled = false;

                    mDeleteVideo.Style["display"] = "none";
                    mDeleteVideo.Disabled = false;

                    //TdSetUpPhoto.Style["display"] = "none";
                    //TdSetUpPhoto.Disabled = false;

                    vDeleteVideo.Style["display"] = "none";
                    vDeleteVideo.Disabled = false;

                    vHiddenPhoto.Style["display"] = "none";
                    vHiddenPhoto.Disabled = false;

                    //TdSetUpVideo.Style["display"] = "none";
                    //TdSetUpVideo.Disabled = false;
                }
                #endregion

                // Universal.JS.RunJS("getSearch()");
            }
        }

        private void bindProjectFile(string strProject)
        {
            int strBranchID = Convert.ToInt32(Session["BRANCH_ID"].ToString());
            BLL.Progect.T_PUB_FILE fileBll = new BLL.Progect.T_PUB_FILE();
            IEnumerable<Model.Progect.T_PUB_FILE> listModle = fileBll.GetProjectFile(strProject);
            listModle = listModle.OrderByDescending(p => p.SETTOPTIME);
            int intCoount = listModle.Count();
            Photocount.Value = intCoount.ToString();
            StringBuilder strHtml = new StringBuilder();
            Model.Progect.T_PUB_FILE model;
            string strPhotoUrl = "";             //图片地址
            int PhototoIndex = 0; ;
            for (int i = 0; i < intCoount; i++)
            {
                model = listModle.ElementAt(i);
                strHtml.Append("<li class=\"list\" onmousemove='mson(this)' onmouseout='msout(this)'>");
                strHtml.Append("<div class=\"p-img\">");
                if (model.TYPE == "0")               //图片
                {
                    strHtml.Append("<a target=\"_blank\" onclick=\"GetView(" + PhototoIndex + ",'','')\">");
                    PhototoIndex = PhototoIndex + 1;
                    strPhotoUrl = strPhotoUrl + model.URLOriginal + ",";
                    strHtml.Append(" <img onerror=\"this.src='http://www.360buy.com/images/none/none_150.gif'\" ");
                    strHtml.Append(string.Format("alt=\"{0}\"", model.FILE_NAME));
                    strHtml.Append(string.Format("width=\"280px\" height=\"150px\" src=\"../{0}\"></a>", model.URLThumbnail.Replace("//", "/")));
                }
                else
                {
                    strHtml.Append("<a target=\"_blank\" onclick=\"GetView(-1,'" + model.URLOriginal.Replace("//", "/") + "','" + model.URLThumbnail.Replace("//", "/") + "')\">");
                    strHtml.Append(" <img onerror=\"this.src='http://www.360buy.com/images/none/none_150.gif'\" ");
                    strHtml.Append(string.Format("alt=\"{0}\"", model.FILE_NAME));
                    strHtml.Append(string.Format("width=\"280px\" height=\"150px\" src=\"../{0}\"></a>", model.URLThumbnail.Replace("//", "/")));
                }
                strHtml.Append("<div class=\"pi7\">");
                strHtml.Append(" </div>");
                strHtml.Append("</div>");
                strHtml.Append("<div class=\"p-name\">");
                strHtml.Append(" <a href=\"\" target=\"_blank\">" + model.FILE_TITEL + "<font style=\"color: #ff0000\"");
                strHtml.Append("class=\"adwords\" name=\"577394\"></font></a></div>");
                strHtml.Append("</li>");
                PageIndex.Value = "1";
                countList.InnerHtml = strHtml.ToString();
                if (strPhotoUrl != "")
                {
                    hidPhotoUrl.Value = strPhotoUrl.Trim(',');
                }
                BLL.SysManage.T_PUB_BRANCH branchBLL = new BLL.SysManage.T_PUB_BRANCH();
                Model.SysManage.T_PUB_BRANCH branchModel = branchBLL.GetModel(strBranchID);
                if (branchModel != null)
                {
                    hidIP.Value = branchModel.NAS_USERID + ":" + branchModel.NAS_PASSWORD + "@" + branchModel.NAS_IP;
                }
                else
                {

                }
            }
        }

        //绑定数据
        private void BindView()
        {
            try
            {
                IEnumerable<Model.View.Exhibition> listModle = Universal.Caches.getCaceData(Session["BRANCH_ID"].ToString());
                listModle = listModle.OrderByDescending(p => p.SETTOPTIME);
                //Universal.JS.Alert(Session["BRANCH_ID"].ToString());
            //    int strBranchID = Convert.ToInt32(Session["BRANCH_ID"].ToString());
                // int strBranchID = 1;
                //  Expression<Func<T, bool>> predicate;
                string strType = hidID.Value;
                IEnumerable<Model.View.Exhibition> resloutList;
                string strProjectType = HIdProjectType.Value.Trim();
                if (strType!="1")
                {
                    if (strProjectType != "")
                    {
                        strProjectType = "," + strProjectType + ",";
                        resloutList = from n in listModle where  n.FILE_FORM.Contains(strType + ",") && ("," + n.PROJECT_TYPE).Contains(strProjectType) select n;
                    }
                    else
                    {
                        resloutList = from n in listModle where n.FILE_FORM.Contains(strType + ",") select n;
                    }
                }
                else
                {
                    if (strProjectType != "")
                    {
                        strProjectType = "," + strProjectType + ",";
                        resloutList = from n in listModle where (n.FILE_FORM.Contains("1,") || n.FILE_FORM == "" || n.FILE_FORM == null) && ("," + n.PROJECT_TYPE).Contains(strProjectType) select n;
                    }
                    else
                    {
                        resloutList = from n in listModle where  (n.FILE_FORM.Contains( "1,")|| n.FILE_FORM == "" || n.FILE_FORM== null) select n;
                    }
                }
              
                //resloutList = resloutList.OrderByDescending(p => p.SETTOPTIME);

                int intCoount = resloutList.Count();
                Photocount.Value = intCoount.ToString();
                //count.InnerText = intCoount.ToString() + "记录   当前第一页";
                intCoount = intCoount < 16 ? intCoount : 16;
                StringBuilder strHtml = new StringBuilder();
                Model.View.Exhibition model;
                string strPhotoUrl = "";             //图片地址
                int PhototoIndex = 0; ;
                for (int i = 0; i < intCoount; i++)
                {
                    model = resloutList.ElementAt(i);
                    strHtml.Append("<li class=\"list\" onmousemove='mson(this)' onmouseout='msout(this)'>");
                    strHtml.Append("<div class=\"p-img\">");
                    if (model.TYPE == "0")               //图片
                    {
                        strHtml.Append("<a target=\"_blank\" onclick=\"GetView(" + PhototoIndex + ",'','')\">");
                        PhototoIndex = PhototoIndex + 1;
                        strPhotoUrl = strPhotoUrl + model.URLOriginal + ",";
                        strHtml.Append(" <img onerror=\"this.src='http://www.360buy.com/images/none/none_150.gif'\" ");
                        strHtml.Append(string.Format("alt=\"{0}\"", model.FILE_NAME));
                        strHtml.Append(string.Format("width=\"280px\" height=\"150px\" src=\"../{0}\"></a>", model.URLThumbnail.Replace("//", "/")));
                    }
                    else
                    {
                        strHtml.Append("<a target=\"_blank\" onclick=\"GetView(-1,'" + model.URLOriginal.Replace("//", "/") + "','" + model.URLThumbnail.Replace("//", "/") + "')\">");
                        strHtml.Append(" <img  onerror=\"this.src='http://www.360buy.com/images/none/none_150.gif'\" ");
                        strHtml.Append(string.Format("alt=\"{0}\"", model.FILE_NAME));
                        strHtml.Append(string.Format("width=\"280px\" height=\"150px\" src=\"../{0}\"></a>", model.URLThumbnail.Replace("//", "/")));
                    }

                    strHtml.Append("<div class=\"pi7\">");
                    strHtml.Append(" </div>");
                    strHtml.Append("</div>");
                    strHtml.Append("<div class=\"p-name\">");
                    strHtml.Append("<font ");
                    strHtml.Append("class=\"adwords\" name=\"577394\">" + model.FILE_TITEL+"</font></div>");
                    strHtml.Append("</li>");
                }
                PageIndex.Value = "1";
                countList.InnerHtml = strHtml.ToString();
                if (strPhotoUrl != "")
                {
                    hidPhotoUrl.Value = strPhotoUrl.Trim(',');
                }
                BLL.SysManage.T_PUB_BRANCH branchBLL = new BLL.SysManage.T_PUB_BRANCH();
                Model.SysManage.T_PUB_BRANCH branchModel = branchBLL.GetModel(Convert.ToInt32(Session["BRANCH_ID"].ToString()));
                if (branchModel != null)
                {
                    hidIP.Value = branchModel.NAS_USERID + ":" + branchModel.NAS_PASSWORD + "@" + branchModel.NAS_IP;
                }
                else
                {

                }
            }
            catch (Exception ex)
            {

                Universal.JS.Alert(ex.Message);
            }
        }

    }
}